package map.blocks.base;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import editor.view.MapOrientation;
import game.models.Player;

import map.MapObject;
import map.PlayerEntity;
import map.RasterCell;

public class MultipleCellBlock extends MapObject {
	
	private int y_cells;
	private int x_cells;
	private MapObject[][] contents;
	
	public MultipleCellBlock(int x_cells, int y_cells, MapObject[][] contents) {
		this.y_cells = y_cells;
		this.x_cells = x_cells;
		this.contents = contents;
	}

	@Override
	public boolean interact(Player actor) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean entered(Player actor) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void paintComponent(Graphics arg0, RasterCell target) {
		target.setAttachedObject(contents[0][0]);
		
		//Y-CELLS (left to right)
		ArrayList<RasterCell> first_ycells = new ArrayList<RasterCell>();
		
		for(int f = 0; f < y_cells-1; f++){
			RasterCell first_neighbor_ycell = null;
			
			if(first_ycells.isEmpty()){
				first_neighbor_ycell = target.getNeighbors().get(MapOrientation.RIGHT);
			} else {
				first_neighbor_ycell = first_ycells.get(f-1).getNeighbors().get(MapOrientation.RIGHT);;
			}
			
			if(first_neighbor_ycell != null){
				first_neighbor_ycell.setAttachedObject(contents[0][f+1]);
				first_ycells.add(first_neighbor_ycell);
			}
		}
		
		//X-CELLS (top to bottom)
		ArrayList<RasterCell> xcells = new ArrayList<RasterCell>();
		
		for(int i = 0; i < x_cells-1; i++){
			RasterCell neighbor_xcell = null;
			if(xcells.isEmpty()){
				neighbor_xcell = target.getNeighbors().get(MapOrientation.BOTTOM);
			} else {
				neighbor_xcell = xcells.get(i-1).getNeighbors().get(MapOrientation.BOTTOM);;
			}
			
			if(neighbor_xcell != null){
				neighbor_xcell.setAttachedObject(contents[i+1][0]);
				xcells.add(neighbor_xcell);
			}
			
			
			
			//Y-CELLS (left to right)
			ArrayList<RasterCell> ycells = new ArrayList<RasterCell>();
			
			for(int j = 0; j < y_cells-1; j++){
				RasterCell neighbor_ycell = null;
				
				if(ycells.isEmpty()){
					neighbor_ycell = neighbor_xcell.getNeighbors().get(MapOrientation.RIGHT);
				} else {
					neighbor_ycell = ycells.get(j-1).getNeighbors().get(MapOrientation.RIGHT);;
				}
				
				if(neighbor_ycell != null){
					neighbor_ycell.setAttachedObject(contents[i+1][j+1]);
					ycells.add(neighbor_ycell);
				}
				

			}

		}

	}

	@Override
	public void deseriralize() {
		// TODO Auto-generated method stub

	}

	@Override
	public MapObject newInstance() {
		return new MultipleCellBlock(this.x_cells, this.y_cells, this.contents);
	}

	@Override
	public boolean enterable(Player actor) {
		return false;
	}

}
